from Tkinter import *
import tkMessageBox
import tkSimpleDialog

"""class NumberEntry(Entry):

    def __init__(self, master, **kw):
        apply(Text.__init__, (self, master), kw)
        self.bind("<Key>", lambda e: "break")
        self.bind("0","""


class ResizeDialog(tkSimpleDialog.Dialog):

    def __init__(self,wpx,hpx,title="Resize and Save"):
        parent = Tk()
        parent.withdraw()
        self.x = self.ix = wpx; self.y = self.iy = hpx
        self.xprop = StringVar(parent); self.xprop.set("100.0")
        self.yprop = StringVar(parent); self.yprop.set("100.0")
        self.xpix = StringVar(parent); self.xpix.set(str(self.x))
        self.ypix = StringVar(parent); self.ypix.set(str(self.y))
        self.upxe1 = self.upxe2 = self.upye1 = self.upye2 = False
        tkSimpleDialog.Dialog.__init__(self,parent,title)

    def body(self,master):
        Label(master,text="X%",width=5).grid(row=1,column=1)
        Label(master,text="Y%",width=5).grid(row=2,column=1)
        Label(master,text="Xpx",width=5).grid(row=1,column=3)
        Label(master,text="Ypx",width=5).grid(row=2,column=3)
        #Label(master,text="Blend resized version",width=15).grid(row=3,column=2,columnspan=3)
        self.xe1 = Entry(master,width=10,textvariable=self.xprop)#,validate="key",validatecommand=self.xe1validate)
        self.ye1 = Entry(master,width=10,textvariable=self.yprop)
        self.xe2 = Entry(master,width=10,textvariable=self.xpix)
        self.ye2 = Entry(master,width=10,textvariable=self.ypix)
        self.xe1.grid(row=1,column=2)
        self.ye1.grid(row=2,column=2)
        self.xe2.grid(row=1,column=4)
        self.ye2.grid(row=2,column=4)
        self.xe1.bind("<Key>",self.upxe1flag)
        self.ye1.bind("<Key>",self.upye1flag)
        self.xe2.bind("<Key>",self.upxe2flag)
        self.ye2.bind("<Key>",self.upye2flag)
        self.xe1.bind_all("<Key>",self.eupdate)
        self.ye1.bind_all("<Key>",self.eupdate)
        self.xe2.bind_all("<Key>",self.eupdate)
        self.ye2.bind_all("<Key>",self.eupdate)
        self.cbblend = Checkbutton(master,text="Use blending (slower)",command=self.toggleblend)
        self.cbblend.select()
        self.cbbval = 1
        self.cbblend.grid(row=3,column=1,columnspan=4)
        return self.xe1

    def toggleblend(self): self.cbbval = 1 - self.cbbval

    #these events hit before the entry update - flag the one that changed
    def upxe1flag(self,event): self.upxe1 = True
    def upxe2flag(self,event): self.upxe2 = True
    def upye1flag(self,event): self.upye1 = True
    def upye2flag(self,event): self.upye2 = True

    def eupdate(self,event): #hits after the entry update - actually change the data
        if self.upxe1: #change xpix to reflect xprop
            try: xs = float(self.xprop.get())/100
            except ValueError: xs = 0
            self.xpix.set(str(int(xs*self.ix)))
            self.upxe1 = False
        if self.upye1: #change ypix to reflect yprop
            try: ys = float(self.yprop.get())/100
            except ValueError: ys = 0
            self.ypix.set(str(int(ys*self.iy)))
            self.upye1 = False
        if self.upxe2: #change xprop to reflect xpix
            try: xs = float(self.xpix.get())
            except ValueError: xs = 0
            self.xprop.set(str(xs/self.ix*100))
            self.upxe2 = False
        if self.upye2: #change yprop to reflect ypix
            try: ys = float(self.ypix.get())
            except ValueError: ys = 0
            self.yprop.set(str(ys/self.iy*100))
            self.upye2 = False

    def validate(self):
        try:
            if int(self.xpix.get()) != 0 and int(self.ypix.get()) != 0:
                return 1
        except: pass
        tkMessageBox.showwarning("Bad input","Invalid picture dimensions.")
        return 0

    def apply(self):
        self.result = (int(self.xpix.get()),int(self.ypix.get()),self.cbbval)


if __name__ == "__main__":
    rootwin = Tk()
    rootwin.withdraw()
    resize = ResizeDialog(640,480)
    print resize.result
